import moment from 'moment';

export default function handleAfter(moduleId, key, value) {
    switch (key) {
        case 'termtype':
            termTypeChange.call(this, moduleId, value);
            break;
        case 'termmonth':
            termMonthChange.call(this);
            break;
        case 'cont_unit':
            countUnitChange.call(this);
            break;
        case 'begindate':
            beginDateChange.call(this);
            break;
        case 'enddate':
            endDateChange.call(this);
            break;
        case 'ifprop':
            ifproChange.call(this, moduleId, value);
            break;
        case 'promonth':
            termMonthChange.call(this, true);
            break;
        case 'prop_unit':
            countUnitChange.call(this, true);
            break;
        case 'probegindate':
            beginDateChange.call(this, true);
            break;
        case 'probenddate':
            endDateChange.call(this);
            break;
        default:
            break;
    }
}

function ifproChange(moduleId, value) {
    let template = this.props.meta.getMeta(), area = moduleId;
    if (template.formrelation && template.formrelation[moduleId]) {
        area = template.formrelation[moduleId][template.formrelation[moduleId].length - 1];
    }
    const promonth = template[area].items.find(item => item.attrcode === 'promonth');
    const prop_unit = template[area].items.find(item => item.attrcode === 'prop_unit');
    const probegindate = template[area].items.find(item => item.attrcode === 'probegindate');
    const probenddate = template[area].items.find(item => item.attrcode === 'probenddate');
    if (value.value === false || value.value === 'N') {
        promonth.required = false;
        prop_unit.required = false;
        probegindate.required = false;
        probenddate.required = false;
        this.props.meta.setMeta(template);
        this.props.form.setFormItemsValue(moduleId, {
            'promonth': {
                value: '',
                display: null
            },
            'prop_unit': {
                value: '',
                display: null
            },
            'probegindate': {
                value: '',
                display: null
            },
            'probenddate': {
                value: '',
                display: null
            }
        });
        this.props.form.setFormItemsDisabled(moduleId, {
            'promonth': true,
            'prop_unit': true,
            'probegindate': true,
            'probenddate': true
        });
    } else {
        const record = this.props.form.getAllFormValue(this.props.areaCode).rows[0].values;
        promonth.required = true;
        prop_unit.required = true;
        probegindate.required = true;
        probenddate.required = true;
        this.props.meta.setMeta(template);
        this.props.form.setFormItemsValue(moduleId, {
            'promonth': {
                value: '',
                display: null
            },
            'prop_unit': prop_unit.options.find(option => option.value === '2'),
            'probegindate': {
                value: record['begindate'].value,
                display: null
            },
            'probenddate': {
                value: '',
                display: null
            }
        });
        this.props.form.setFormItemsDisabled(moduleId, {
            'promonth': false,
            'prop_unit': false,
            'probegindate': false,
            'probenddate': false
        });
    }
}

function termTypeChange(moduleId, value) {
    if (value.value === 'CM02') {
        let template = this.props.meta.getMeta(), area = moduleId;
        if (template.formrelation && template.formrelation[moduleId]) {
            area = template.formrelation[moduleId][template.formrelation[moduleId].length - 1];
        }
        const termmonth = template[area].items.find(item => item.attrcode === 'termmonth');
        const enddate = template[area].items.find(item => item.attrcode === 'enddate');
        termmonth.required = false;
        enddate.required = false;
        this.props.meta.setMeta(template);
        this.props.form.setFormItemsValue(moduleId, {
            'termmonth': {
                value: '',
                display: null
            },
            'enddate': {
                value: '',
                display: null
            }
        });
        this.props.form.setFormItemsDisabled(moduleId, {
            'termmonth': true,
            'enddate': true
        });
    } else {
        let template = this.props.meta.getMeta(), area = moduleId;
        if (template.formrelation && template.formrelation[moduleId]) {
            area = template.formrelation[moduleId][template.formrelation[moduleId].length - 1];
        }
        const termmonth = template[area].items.find(item => item.attrcode === 'termmonth');
        const enddate = template[area].items.find(item => item.attrcode === 'enddate');
        termmonth.required = true;
        enddate.required = true;
        this.props.meta.setMeta(template);
        this.props.form.setFormItemsDisabled(moduleId, {
            'termmonth': false,
            'enddate': false
        });
    }
}

function termMonthChange(isPro) {
    const {form, areaCode} = this.props;
    const record = form.getAllFormValue(areaCode).rows[0].values;
    if (record[isPro ? 'promonth' : 'termmonth'].value == null ||
        (!record[isPro ? 'probegindate' : 'begindate'].value && !record[isPro ? 'probenddate' : 'enddate'].value))
        return;
    if (record[isPro ? 'probegindate' : 'begindate'].value) {
        const endDate = getEndDate(
            record[isPro ? 'promonth' : 'termmonth'].value,
            record[isPro ? 'probegindate' : 'begindate'].value,
            record[isPro ? 'prop_unit' : 'cont_unit'].value
        );
        form.setFormItemsValue(areaCode, {
            [isPro ? 'probenddate' : 'enddate']: {
                value: endDate,
                display: null
            }
        })
    } else {
        const beginDate = getBeginDate(
            record[isPro ? 'promonth' : 'termmonth'].value,
            record[isPro ? 'probenddate' : 'enddate'].value,
            record[isPro ? 'prop_unit' : 'cont_unit'].value
        );
        form.setFormItemsValue(areaCode, {
            [isPro ? 'probegindate' : 'begindate']: {
                value: beginDate,
                display: null
            }
        })
    }
}

function countUnitChange(isPro) {
    const {form, areaCode} = this.props;
    const record = form.getAllFormValue(areaCode).rows[0].values;
    if (record[isPro ? 'promonth' : 'termmonth'].value == null ||
        (!record[isPro ? 'probegindate' : 'begindate'].value && !record[isPro ? 'probenddate' : 'enddate'].value))
        return;
    if (record[isPro ? 'probegindate' : 'begindate'].value) {
        const endDate = getEndDate(
            record[isPro ? 'promonth' : 'termmonth'].value,
            record[isPro ? 'probegindate' : 'begindate'].value,
            record[isPro ? 'prop_unit' : 'cont_unit'].value
        );
        form.setFormItemsValue(areaCode, {
            [isPro ? 'probenddate' : 'enddate']: {
                value: endDate,
                display: null
            }
        })
    } else {
        const beginDate = getBeginDate(
            record[isPro ? 'promonth' : 'termmonth'].value,
            record[isPro ? 'probenddate' : 'enddate'].value,
            record[isPro ? 'prop_unit' : 'cont_unit'].value
        );
        form.setFormItemsValue(areaCode, {
            [isPro ? 'probegindate' : 'begindate']: {
                value: beginDate,
                display: null
            }
        })
    }
}

function beginDateChange(isPro) {
    const {form, areaCode} = this.props;
    const record = form.getAllFormValue(areaCode).rows[0].values;
    if (record[isPro ? 'promonth' : 'termmonth'].value == null) return;
    if (record[isPro ? 'probegindate' : 'begindate'].value) {
        const endDate = getEndDate(
            record[isPro ? 'promonth' : 'termmonth'].value,
            record[isPro ? 'probegindate' : 'begindate'].value,
            record[isPro ? 'prop_unit' : 'cont_unit'].value
        );
        form.setFormItemsValue(areaCode, {
            [isPro ? 'probenddate' : 'enddate']: {
                value: endDate,
                display: null
            }
        })
    }
}

function endDateChange(isPro) {
    const {form, areaCode} = this.props;
    const record = form.getAllFormValue(areaCode).rows[0].values;
    if (record[isPro ? 'probegindate' : 'begindate'].value == null) return;
    if (record[isPro ? 'probenddate' : 'enddate'].value) {
        const termmonth = getGapDate(
            record[isPro ? 'probegindate' : 'begindate'].value,
            record[isPro ? 'probenddate' : 'enddate'].value,
            record[isPro ? 'prop_unit' : 'cont_unit'].value
        );
        form.setFormItemsValue(areaCode, {
            [isPro ? 'promonth' : 'termmonth']: {
                value: termmonth,
                display: null
            }
        })
    }
}

function getEndDate(itermmonth = 0, startdate, unit) {
    //"1":"年" "2":"月" "3": "天"
    let nextDate = '';
    if (unit === '1') {
        nextDate = moment(startdate).add(Number(itermmonth), 'y').subtract(Number(1), 'd');
    }
    if (unit === '2') {
        nextDate = moment(startdate).add(Number(itermmonth), 'M').subtract(Number(1), 'd');
    }
    if (unit === '3') {
        nextDate = moment(startdate).add(Number(itermmonth), 'd');
    }
    return nextDate.format('YYYY-MM-DD');
}

function getBeginDate(itermmonth = 0, enddate, unit) {
    //"1":"年" "2":"月" "3": "天"
    let nextDate = '';
    if (unit === '1') {
        nextDate = moment(enddate).subtract(Number(itermmonth), 'y').add(Number(1), 'd');
    }
    if (unit === '2') {
        nextDate = moment(enddate).subtract(Number(itermmonth), 'M').add(Number(1), 'd');
    }
    if (unit === '3') {
        nextDate = moment(enddate).subtract(Number(itermmonth), 'd');
    }
    return nextDate.format('YYYY-MM-DD');
}

function getGapDate(begindate, enddate, unit) {
    //"1":"年" "2":"月" "3": "天"
    let beginDate = new Date(begindate).getTime(), endDate = new Date(enddate).getTime(), gap = 0;
    if (unit === '1') {
        gap = (endDate - beginDate) / (1000 * 60 * 60 * 24 * 365);
    }
    if (unit === '2') {
        gap = (endDate - beginDate) / (1000 * 60 * 60 * 24 * 30);
    }
    if (unit === '3') {
        gap = (endDate - beginDate) / (1000 * 60 * 60 * 24);
    }
    return Math.round(gap);
}
